package com.rqbl.service.impl;


import com.rqbl.entity.Interest;
import com.rqbl.mapper.UserinterestMapper;
import com.rqbl.myutils.Transfer;
import com.rqbl.service.UserinterestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bruce
 * @since 2024-07-19
 */
@Service
public class UserinterestServiceImpl  implements UserinterestService {

    @Autowired
    private UserinterestMapper userinterestMapper;


    @Override
    public List<Transfer> getInterestByuid(Integer uid) {
        List<Interest> interests = userinterestMapper.getInterestByuid(uid);
        return interests.stream().map(item ->
                new Transfer(item.getIid().toString(), item.getItype())).collect(java.util.stream.Collectors.toList());
    }

    @Override
    public Integer updateInterestByuid(List<Transfer> transfers, Integer uid) {
        List<Interest> interests = userinterestMapper.getInterestByuid(uid);
        //存储新标签
        HashSet<Integer> hashSet1 = new HashSet<>();
        //将新标签加入数据源
        for(Transfer transfer : transfers){
            hashSet1.add(Integer.parseInt(transfer.getValue()));
        }


        //存储已有的标签
        HashSet<Integer> hashSet2 = new HashSet<>();
        //排除删掉的标签
        for(Interest interest : interests){
            if(!hashSet1.contains(interest.getIid())){
                userinterestMapper.deleteBarInterest(uid, interest.getIid());
            } else {
                hashSet2.add(interest.getIid());
            }
        }

        System.out.println(hashSet2.toString());
        System.out.println(hashSet1.toString());
        //将新标签加入数据库
        for(Integer in : hashSet1){
            if(!hashSet2.contains(in)){
                System.out.println("新增标签"+in);
                userinterestMapper.insertBarinterest(uid, in);
            }
        }
        return 1;
    }
}
